package io.gitee.chearnee.fw.openapi.controller;

import cn.hutool.core.util.RandomUtil;
import io.gitee.chearnee.fw.openapi.advice.EnableOpenApiResponseAdvice;
import io.gitee.chearnee.fw.openapi.annotation.OpenApiAuth;
import io.gitee.chearnee.fw.openapi.business.BusinessManager;
import io.gitee.chearnee.fw.openapi.business.entity.Application;
import io.gitee.chearnee.fw.openapi.business.entity.AuthorizedResources;
import io.gitee.chearnee.fw.openapi.business.entity.Company;
import io.gitee.chearnee.fw.openapi.constant.enums.CommonStatusEnum;
import io.gitee.chearnee.fw.openapi.constant.enums.ContractStatusEnum;
import io.gitee.chearnee.fw.openapi.model.InitApplication;
import io.gitee.chearnee.fw.openapi.processor.OpenApiProcessor;
import io.gitee.chearnee.fw.web.annotation.EnableResponseAdvice;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/9/29 13:45
 */
@RestController
@RequestMapping("/build")
@AllArgsConstructor
@EnableResponseAdvice(enable = false)
@EnableOpenApiResponseAdvice
@OpenApiAuth(ignoreAccessTokenAuth = true, ignoreAuth = true)
public class tempBuildController {

  private final BusinessManager businessManager;
  private final OpenApiProcessor openApiProcessor;

  @PostMapping("company")
  public InitApplication buildCompany(@RequestBody InitApplication initApplication) {
    if (StringUtils.hasText(initApplication.getToken()) && initApplication.getToken()
        .equals("fw666")) {
      Company company = initApplication.getCompany();
      Application application = initApplication.getApplication();
      AuthorizedResources authorization = initApplication.getAuthorization();
      if (company != null) {
        if (company.getId() != null && company.getId() > 0) {

          businessManager.updateCompany(company);
        } else {
          company.setContractStatus(ContractStatusEnum.VALID_CONTRACT);
          company.setStatus(CommonStatusEnum.ENABLE);
          businessManager.saveCompany(company);

        }
      }
      return initApplication;
    }
    return null;
  }

  @PostMapping("application")
  public InitApplication buildApplication(@RequestBody InitApplication initApplication) {
    if (StringUtils.hasText(initApplication.getToken()) && initApplication.getToken()
        .equals("fw666")) {
      Application application = initApplication.getApplication();
      if (application != null) {
        application.setAccessToken(null);
        application.setExpiresTime(null);
        if (StringUtils.hasText(application.getAppId())) {
          businessManager.updateApplication(application);
          openApiProcessor.refreshToken(application.getAppId());
        } else {
          String appId = RandomUtil.randomString(16);
          application.setAppId("fw" + appId);
          String appkey = RandomUtil.randomString(32);
          application.setAppKey(appkey);
          application.setStatus(CommonStatusEnum.ENABLE);
          businessManager.saveApplication(application);
        }
      }
      return initApplication;
    }
    return null;
  }

  @PostMapping("authorizedResources")
  public InitApplication buildAuthorizedResources(@RequestBody InitApplication initApplication) {
    if (StringUtils.hasText(initApplication.getToken()) && initApplication.getToken()
        .equals("fw666")) {
      AuthorizedResources authorization = initApplication.getAuthorization();
      if (authorization != null) {
        if (authorization.getId() != null && authorization.getId() > 0) {
          businessManager.updateResources(authorization);
          openApiProcessor.refreshAuthResource(authorization.getAppId());
        } else {
          businessManager.saveAuthorizedResources(authorization);
        }
      }
      return initApplication;
    }
    return null;
  }
}
